package com.xindaoapp.happypet.viewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

/**
 * 切换tab页面自定义viewpager，可以通过addTabs增加tab自动切换
 * 
 * @author chenghao
 * 
 */
@Deprecated
public class SwitchTabViewPager extends ViewPager {

    private View[] mViews;

    private ViewPagerWindow[] mWindows;
    private OnPageChangeListener mPageChangeListener;
    private int mDefaultTabIndex;

    public SwitchTabViewPager(Context context) {
        super(context);
    }

    public SwitchTabViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void addTabs(View[] views) {
        addTabs(views, 0, false, false);
    }

    public void addTabs(View[] views, int defaultTabIndex) {
        addTabs(views, defaultTabIndex, false, false);
    }

    public void addTabs(View[] views, int defaultTabIndex, boolean autoSwitch) {
        addTabs(views, defaultTabIndex, autoSwitch, false);
    }

    /**
     * 添加tab切换
     * 
     * @param views 需要切换的tab布局
     * @param defaultTabIndex 当前默认的tab
     * @param autoSwitch 是否自动切换，这样会监听OnPageChangeListener，需要手动调用addOnPageChangeListener监听
     * @param isAutoDestoryPage 是否自动销毁页面
     */
    public void addTabs(View[] views, int defaultTabIndex, boolean autoSwitch,
            boolean isAutoDestoryPage) {
        this.mViews = views;
        initListener();
        mDefaultTabIndex = defaultTabIndex;
        setTabItemSelected(defaultTabIndex);
        setOnPageChangeListener(mOnPageChangeListener);
        if (!isAutoDestoryPage) {
            // 设置之后，切换tab页面不会销毁页面
            setOffscreenPageLimit(Integer.MAX_VALUE);
        }
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
        setTabItemSelected(item);
    }

    @Override
    public void setOnPageChangeListener(OnPageChangeListener listener) {
        super.setOnPageChangeListener(listener);
    }

    /**
     * 添加页面监听
     * 
     * @param listener
     */
    public void addOnPageChangeListener(OnPageChangeListener listener) {
        mPageChangeListener = listener;
    }

    private final OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            setTabItemSelected(position);
            // 切换tab时回调去刷新界面
            mWindows[position].loadData();
            if (null != mPageChangeListener) {
                mPageChangeListener.onPageSelected(position);
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            if (null != mPageChangeListener) {
                mPageChangeListener.onPageScrolled(arg0, arg1, arg2);
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            if (null != mPageChangeListener) {
                mPageChangeListener.onPageScrollStateChanged(arg0);
            }
        }
    };

    private void initListener() {
        for (int i = 0; i < mViews.length; i++) {
            final int temp = i;
            mViews[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setTabSelector(temp);
                    if (tabSelector != null) {
                        tabSelector.onSelector0(temp);
                    }
                }
            });
        }
    }

    /**
     * 改变Tab和 内容也的选中
     * 
     * @param position
     */
    public void setTabSelector(int position) {
        setCurrentItem(position);
        // setTabItemSelected(position);
    }


    public void setTabItemSelected(int position) {
        for (int i = 0; i < mViews.length; i++) {
            if (position == i) {
                mViews[i].setSelected(true);
            } else {
                mViews[i].setSelected(false);
            }
        }
    }

    public void addPagerWindows(ViewPagerWindow[] windows) {
        mWindows = windows;
    }

    @Override
    public void setAdapter(PagerAdapter arg0) {
        if (null == mWindows) {
            throw new RuntimeException("please use addWindows(ViewPagerWindow[] windows)");
        }
        super.setAdapter(arg0);
        mWindows[mDefaultTabIndex].loadData();
    }

    /*
     * public void setAdapter(ViewPagerWindow[] windows, PagerAdapter adapter) { mWindows = windows;
     * super.setAdapter(adapter); setCurrentItem(mDefaultTabIndex); mWindows = windows;
     * selected(mDefaultTabIndex); mWindows[mDefaultTabIndex].onLoadDatas(); }
     */
    public TabSelector tabSelector;

    public interface TabSelector {
        void onSelector0(int position);
    }

    public void setOnTabSelector(TabSelector selector) {
        this.tabSelector = selector;
    }
}
